package com.menhai.personmanagement.services;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.HtEquipment;
import com.menhai.personmanagement.entity.SysRole;
import com.menhai.personmanagement.mapper.HtEquipmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author shikun
 * @since 2021-12-07
 */
 @Service
public class IHtEquipmentService {

    @Autowired
    private HtEquipmentMapper htEquipmentMapper;

    public int addEquipment(HtEquipment htEquipment){
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        htEquipment.setCreateTime(sp.format(new Date()));
        htEquipment.setId(UUID.randomUUID().toString());
        return htEquipmentMapper.insert(htEquipment);
    }

    public Page<HtEquipment> selectAll(int pagesize,int size){
        Page<HtEquipment> page = new Page<>(pagesize,size);
        return htEquipmentMapper.selectPage(page, null);
    }
    public List<HtEquipment> selectAll(){
        return htEquipmentMapper.selectList(null);
    }

    public int editEquipment(HtEquipment htEquipment){
        return htEquipmentMapper.updateById(htEquipment);
    }

    public List findEquipmentByname(String name){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        return htEquipmentMapper.selectList(queryWrapper);
    }
    public List findEquipmentByMac(String mac){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mac",mac);
        return htEquipmentMapper.selectList(queryWrapper);
    }
    public Page<HtEquipment> findEquipmentByMacOrSn(String content,int pagesize,int size){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("mac",content);
        queryWrapper.or();
        queryWrapper.like("sncode", content);
        queryWrapper.orderByDesc("create_time");
        Page<HtEquipment> page = new Page<>(pagesize,size);
        return htEquipmentMapper.selectPage(page, queryWrapper);

    }

    public int delEquipment(String id){
        return htEquipmentMapper.deleteById(id);
    }
}
